/**
 * Object ready
 */
ninejs.extend(ninejs, function( exports ) {
    var ready = function( onready, config ) {
		var isReady = false,
		ua = navigator.userAgent.toLowerCase(),
		browser = {
			msie: /msie/gi.test(ua),
			webkit: /webkit/gi.test(ua),
			firefox: /firefox/gi.test(ua),
			version: /[ ](\d+)/.exec(ua)[1]
		},
		doReady = function () {
			if( isReady ) return;
			isReady = true;	//确保onready只执行一次

			onready();
		},
		_ieReady = function () {
			if ( isReady ) return;
			
			try {
				document.documentElement.doScroll("left");
			} catch( error ) {
				setTimeout(_ieReady, 0);
				return;
			}

			doReady();
		},
		_wkReady = function () {
			if ( isReady ) return;
			if ( /loaded|complete/.test(document.readyState) ) {
				doReady();
			} else {
				setTimeout(_wkReady, 0);
			}
		},
		_ffReady = function () {
			document.removeEventListener("DOMContentLoaded", _ffReady, false);
			doReady();
		};

		//设置是否在FF下使用DOMContentLoaded（在FF2下的特定场景有Bug） 
		this.conf = { 
			enableMozDOMReady: true
		};

		if ( config ) {
			for ( var p in config ) {
				this.conf[p] = config[p];
			}
		}

		/********* For IE ************/
		if( browser.msie ) {
			_ieReady();
			window.attachEvent('onload', doReady);
		}
		/********* For Webkit ********/
		else if ( browser.webkit && browser.version < 525 ) {
			_wkReady();
			window.addEventListener('load', doReady, false);
		}
		/********* For Firefox Opera 高版webkit 其他 ************/
		else {
			if ( !browser.firefox || browser.version != 2 || this.conf.enableMozDOMReady ) {
				document.addEventListener("DOMContentLoaded", _ffReady, false);
			}
			window.addEventListener('load', doReady, false);
		}
	};

	exports.ready = ready;
});